package com.hzgj.bcl.soa.controller.logic;

import com.google.common.collect.Maps;
import com.hzgj.bcl.soa.command.CommandsInitializer;
import com.hzgj.bcl.soa.command.SoaCommand;
import com.hzgj.bcl.soa.util.Pair;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by warning5 on 17-6-1.
 */
public class LinePageLogic implements PageLogic {
    @Override
    public List<String> getPages() {
        return Arrays.asList("request-time", "request-error", "request-count");
    }

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/DD/YYYY h:mm");

    @Override
    public void handle(HttpServletRequest request, Model model) {
        CommandsInitializer.InternalCommand soaCommand = CommandsInitializer.getCommand("invoke_service");
        SoaCommand.CommandOptions commandOptions = new SoaCommand.CommandOptions();
        commandOptions.params = Maps.newHashMap();
        commandOptions.params.put("firstService", new String[]{"1"});
        Map<String, Object> result = (Map<String, Object>) soaCommand.invoke0(commandOptions);
        Pair pair = (Pair) result.get("services");
        if (pair != null) {
            model.addAttribute("services", pair.getLeft());
            model.addAttribute("methods", pair.getRight());
        }
        model.addAttribute("nodes", result.get("nodes"));
        Calendar calendar = Calendar.getInstance();
        Date end = calendar.getTime();
        calendar.add(Calendar.HOUR_OF_DAY, -1);

        model.addAttribute("span", simpleDateFormat.format(calendar.getTime()) + " - " + simpleDateFormat.format(end));
    }
}
